package yby;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/user/del")
public class Insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("n");
        String username = req.getParameter("u");
        String id = req.getParameter("i");
        String sql1 = "INSERT INTO `usertable` (username,`password`,`name`)VALUES (?,MD5('123'),?)";
        String sql2 = "UPDATE `usertable` SET `name` = ?,username=? WHERE `id`=?";
        //String sql3 = "SELECT `username` FROM `usertable` ";

        if (id!=null && !"null".equals(id)) {
            JdbcUtils.modify(sql2,username,name,id);
        } else {
            JdbcUtils.modify(sql1, username, name);
        }


//        int i=0;
//        List<String> listUserName = JdbcUtils.selects(sql1, String.class);
//        for (String user :listUserName) {
//            if (user.equals(username)) {
//                i=1;
//                break;
//            }
//        }


//        JdbcUtils.modify(sql1, name, username);

        resp.sendRedirect("/yby1/user-list.jsp");

    }
}
